﻿ /*************************************************************************
File:   as/log.as
Author: Duykham
UpDate: 05/11/2006
TODO: Logging
/*************************************************************************/
trace ("#included: log.as");
///////////////////////////////////////////////////////////////////////////
//TODO: variables

//Tao mot ket noitoi FMS//
myConnection2 = new NetConnection ();
myConnection2.connect (_root.CFG.fmsApp);
//myConnection2.connect ("rtmp://"+_root.CFG.IPaddress+"/ProconDemoApp");

//Tao mot stream moi de nhan luong video/audio//
in_ns = new NetStream (myConnection2);



//Cac bien//

var flag:Boolean = false;
var oldDate = clock.strDate;
var myList = new Array ();
var changeList = new Object ();
var rsoMessages ;

var rsoDateMessages;
var myLoadVars = new LoadVars (); // tao mot doi tuong LoadVar moi.

//Cac doi tuong Component//
var list_cb : mx.controls.List;

var report_txt : mx.controls.TextArea;


////////////////////////////////////////////
/////////Khoi tao gia tri dau///////////////
////////////////////////////////////////////


//myLoadVars.load ("http://"+_root.CFG.IPaddress+"/procon/list.php?dir="+_root.CFG.fmsDir+"&value=sharedobjects\\_definst_\\log");
myLoadVars.load (_root.CFG.webAdd+"list.php?dir="+_root.CFG.fmsDir+"&value=sharedobjects\\_definst_\\log");

//rsoMessages
rsoMessages= SharedObject.getRemote ("log/" + clock.strDate, myConnection2.uri, true);
rsoMessages.connect (myConnection2);


rsoDateMessages = clock.strDate;


//////////////////////////////////////////////
////////        Cac ham          /////////////
//////////////////////////////////////////////


//TODO:Lay danh sach Logs tu file .php, dua vao component list_cb
myLoadVars.onLoad = function (success)
{
	if (success)
	{
		var newItem = new Object ();
		var nameStr = new String ();
		var typeStr = new String ();
		for (var role in this)
		//trong khi dang con duyet den phan tu role trong mang myLoadVars
		if (role != "onLoad" && this [role] != "undefined")
		{
			nameStr = this [role];
			//Lay ra ten cua loai file VD: .flv
			typeStr = nameStr.substr (nameStr.indexOf (".") , 4);
			//loai bo duoi .flv trong ten file
			nameStr = nameStr.substr (0, nameStr.indexOf ("."));
			//text = text + nameStr + typeStr+ newline;
			if (typeStr == ".fso")
			{
				newItem.label = nameStr;
				newItem.data = nameStr;
				//trace("newItem.label: "+ newItem.label);
				_root.myList.push ( 
				{
					label : newItem.label, data : newItem.data
				}
				);
				//trace("myList: "+myList);
				
			} 
			else
			{
				//do nothing
				
			}
		} 
		else
		{
			//do nothing
			
		}
		formatDate(myList);		
		//De List box hien thi ngay hien tai
		for (var i:Number = 0; i < list_cb.length; i++)
		//for (var i:Number = 0; i < 7; i++)
		{
			if (list_cb.getItemAt(i).label == clock.strDate)
			//if (folder_cb.getItemAt(i) == "14-06-2006")
			{
				list_cb.selectedIndex = i;
				break;
			}
		}
	}
};


function formatDate (dateArray: Array)
{
	var newDateArray:Array = new Array();
	for (i=0; i<dateArray.length; i++)
	{
		var newArr = dateArray[i].label.split("-");
		//trace ("newArr: "+newArr[0]);
		newDateArray.push({year: newArr[2], month: newArr[1], date: newArr[0], string:newArr[2]+newArr[1]+newArr[0]});
	};
	newDateArray.sortOn("string");
	newDateArray.reverse();
	trace("newDateArray.length: "+newDateArray.length);
	var newFolderList = new Array();
	for (j=0; j<newDateArray.length; j++)
	{
		var dat = newDateArray[j].date;
		var mon = newDateArray[j].month;
		var yea = newDateArray[j].year;
		var str = dat+"-"+mon+"-"+yea;
		newFolderList.push ({label: str, data: str})
	};
	list_cb.dataProvider = newFolderList;
};


//TODO:Restart lai ket noi (khi chuyen ngay)
restart = function ()
{
	my_video.attachVideo (null);
	in_ns.close ();
	in_ns = new NetStream (myConnection2);
	in_ns.play (clock.strDate);
	my_video.attachVideo (in_ns);
	oldDate = clock.strDate;
};



//TODO:Exit button
exit_btn.onRelease = function()
{		
		my_video.attachVideo (null);
		my_video.clear ();
		//in_ns.attachAudio (null);
		//in_ns.attachVideo (null);
		// Stop publishing and close the stream.
		in_ns.play (false);
		in_ns.close ();
		get_ns.close ();
		rsoMessages.connect (false);
		myConnection2.close ();
		gotoAndPlay ("main");
};


//TODO:Load du lieu tu Log vao report_txt, sau khi chon ngay tu list_cb
changeList.change = function (event)
{
	rsoDateMessages = event.target.value;
	rsoMessages = SharedObject.getRemote ("log/" + rsoDateMessages, myConnection2.uri, true);
	rsoMessages.connect (myConnection2);
};
list_cb.addEventListener ("change", changeList);


//TODO:When the RSO synchronizes, assign the data to the data provider for the List.
rsoMessages.onSync = function (info)
{
	var mList = this.data.logs;
	var i=0;
	report_txt.text = "";
	report_txt.text = "DATE : " + rsoDateMessages + newline + "--------------------------------------------------------------------------" + newline;
	while (mList[i] != undefined){
	//while (i=0; i < mList.length(); i++){
		report_txt.text = report_txt.text + newline + "Time: " + mList[i].label + newline + "Motion level:  " + mList[i].data + newline;
		i++;
	}
};


////////
view_btn.onRelease = function (info)
{
	var mList = rsoMessages.data.logs;
	var i=0;
	report_txt.text = "";
	report_txt.text = "DATE : " + list_cb.value  + newline + "--------------------------------------------------------------------------" + newline;
	while (mList[i] != undefined){
	//while (i=0; i < mList.length(); i++){
		report_txt.text = report_txt.text + newline + "Time: " + mList[i].label + newline + "Motion level:  " + mList[i].data + newline;
		i++;
	}
};
//////////

//TODO:OnEnterFrame
this.onEnterFrame = function ()
{
	if (oldDate != clock.strDate)
	{
		//Restart lai ket noi (khi chuyen ngay)
		restart ();
	}
};

status_txt = "Daily log";
list_cb.setStyle ("themeColor", "haloBlue");
list_cb.rowHeight = 30;